﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NBusiness.CodeDom.Services;
using NBusiness.CodeDom;
using System.Reflection;
using System.IO;
using NBusiness.ESharp;
using NBusiness.Frameworks.Csla;

namespace NBusiness.VisualStudio
{
    class ESharpFrameworkService : IFrameworkService
    {
        ESharpReflectionService _reflectionService;
        List<IFramework> _frameworks = new List<IFramework>();

        public ESharpFrameworkService(ESharpReflectionService reflectionService)
        {
            _reflectionService = reflectionService;

            _frameworks.Add(new CslaFramework(_reflectionService));
        }

        #region IFrameworkService Members

        public EntityTemplate[] GetTemplates()
        {
            TypeData[] templateTypes = _reflectionService.FindAssignableFrom(new TypeData(typeof(ITemplate)));
            var entityTemplates = from td in templateTypes
                                  select new ESharpTemplate
                                  {
                                      Name = td.FullName,
                                      Type = td
                                  };

            return entityTemplates.ToArray();
        }

        public EntityValidation[] GetValidationRules()
        {
            return (from f in _frameworks
                    from r in f.GetValidationRules()
                    select r).ToArray();
        }

        public EntityAccess[] GetAccessRules()
        {
            return (from f in _frameworks
                    from r in f.GetAccessRules()
                    select r).ToArray();
        }

        public EntityAuthorization[] GetAuthorizationRules()
        {
            return (from f in _frameworks
                    from r in f.GetAuthorizationRules()
                    select r).ToArray();
        }

        #endregion
    }
}
